from typing import Union, Dict

import aioredis


class RedisRepository:
    def __init__(self, pool: aioredis.Redis):
        self.redis_client: aioredis.Redis = pool

    def _create_pipeline(self):
        """创建异步Pipeline实例"""
        return self.redis_client.pipeline(
            transaction=False,
            shard_hint=None
        )

    async def _get(self, key: str) -> str:
        b = await self.redis_client.get(key)
        return b and b.decode()

    async def _set(self, key: str, value: Union[int, str]):
        await self.redis_client.set(key, value)

    async def _del(self, key: str):
        await self.redis_client.delete(key)

    async def _incr(self, key: str, value: int):
        await self.redis_client.incr(key, value)

    async def _decr(self, key: str, value: int):
        await self.redis_client.decr(key, value)

    async def _hset(self, key: str, values: Dict[str, aioredis.connection.EncodableT]):
        await self.redis_client.hset(key, mapping=values)
